#include "EditorMaterialPageModel.h"
#include "Window/EditorWindowGroupModel.h"

#include "Material/Material.h"
#include "MaterialGraph/MaterialGraphModel.h"

TSharedObjectPtr<REditorPageModel> REditorMaterialPageModel::ShareClone(RObject *InOwner) const
{
    auto NewModel = NewObject<REditorMaterialPageModel>(InOwner);
    NewModel->Material = Material;
    NewModel->FilePath = FilePath;
    NewModel->MaterialGraph = MaterialGraph;
    return NewModel;
}

bool REditorMaterialPageModel::HasUnsavedData() const
{
    return MaterialGraph->HasUnsavedData();
}

void REditorMaterialPageModel::LowLevelSave()
{
    MaterialGraph->Save(Material);
}

TSharedObjectPtr<RObject> REditorMaterialPageModel::GetFocuseObject() const
{
    return Material;
}

TSharedObjectPtr<REditorMaterialPageModel> REditorMaterialPageModel::Create(
    const SPath &InFilePath, TSharedObjectPtr<RMaterial> InMaterial, TSharedObjectPtr<RMaterialGraphModel> InMaterialGraph)
{
    auto Model= NewObject<REditorMaterialPageModel>(REditorWindowGroupModel::Get());
    Model->FilePath = InFilePath;
    Model->Material = InMaterial;
    Model->MaterialGraph = InMaterialGraph;
    return Model;
}

void REditorMaterialPageModel::UpdateMaterial()
{
    MaterialGraph->RegenerateMaterial(Material);
}

bool REditorMaterialPageModel::CanUndo() const
{
    return MaterialGraph->CanUndo();
}

bool REditorMaterialPageModel::Undo()
{
    if(CanUndo())
    {
        MaterialGraph->Undo();
        return true;
    }
    return false;
}

bool REditorMaterialPageModel::CanRedo() const
{
    return MaterialGraph->CanRedo();
}

bool REditorMaterialPageModel::Redo()
{
    if(!CanRedo())
    {
        return false;
    }
    MaterialGraph->Redo();
    return true;
}
